package com.ruoyi.serviceItem.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.serviceItem.domain.ServicePackage;
import com.ruoyi.serviceItem.service.ServicePackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/package")
public class ServicePackageController {
    @Autowired
    private ServicePackageService servicePackageService;

    @GetMapping("/list")
    public R selectServicePackageList(int storeId){
        return R.ok(servicePackageService.selectServicePackageList(storeId));
    }


    @PostMapping
    public R add(@RequestBody ServicePackage servicePackage) {
        servicePackage.setStoreId(getStoreId()); // 从上下文中获取门店ID
        return R.ok(servicePackageService.savePackage(servicePackage));
    }

    @PutMapping
    public R edit(@RequestBody ServicePackage servicePackage) {
        servicePackage.setStoreId(getStoreId());
        return R.ok(servicePackageService.savePackage(servicePackage));
    }

    @DeleteMapping("/{packageId}")
    public R delete(@PathVariable Long packageId) {
        try {
            return R.ok(servicePackageService.deletePackageById(packageId));
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        }
    }

    // 辅助方法：获取当前门店ID（根据实际鉴权方式实现）
    private Long getStoreId() {
        // 示例：从安全上下文中获取
        return 1L;
    }


}
